package com.kehutong.wwin.service.controller;

import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import com.kehutong.wwin.service.entity.Suite;

@MvcAction
@ReqMapping("/admin/wxin/suite")
public class SuiteController {
	
	@Inject
	private JdbcSession jdbcSession;
	
	@HasPermission("wwin:suite:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
		return jdbcSession.findPage(Suite.class)
				.eq(jsonObject, "appId")
				.like(jsonObject, "name")
				.eq("deleted", false)
				.page(jsonObject)
				.exe();
    }
	
    @HasPermission("wwin:suite:view")
    @ReqMapping("/get")
    public Object get(Suite item) {
    	return item;
    }
    
    @Bean(newInstance=true, copy=true)
    @HasPermission("wwin:suite:edit")
    @ReqMapping("/save")
    public Object save(Suite item) throws Exception {
    	jdbcSession.insert(item);
    	return Result.success(item);
    }
    
    @Bean(copy=true)
    @HasPermission("wwin:suite:edit")
    @ReqMapping("/update")
    public Object update(Suite item) throws Exception {
    	
    	jdbcSession.updateById(item);
    	return Result.success(item);
    }

    @HasPermission("wwin:suite:edit")
    @ReqMapping("/delete")
    public Object delete(Suite item) throws Exception {
    	if (Objects.nonNull(item)) {
    		item.setDeleted(true);
    		jdbcSession.updateById(item);
    	}
    	
    	return Result.success();
    }
}
